package com.meituan.catering.management.product.biz.model.converter;

import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.product.api.http.model.response.ProductDetailHttpResponse;
import com.meituan.catering.management.product.api.http.model.response.ProductPageHttpResponse;
import com.meituan.catering.management.product.biz.model.ProductBO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 从其他数据模型向商品Http模型的转换器
 */
public abstract class ProductHttpVOConverter {

    public static ProductDetailHttpResponse fromBO(ProductBO productBO) {
        if (productBO == null) {
            return null;
        } else {
            ProductDetailHttpResponse httpResponse = new ProductDetailHttpResponse();
            httpResponse.setId(productBO.getId());
            httpResponse.setTenantId(productBO.getTenantId());
            httpResponse.setVersion(productBO.getVersion());
            httpResponse.getAuditing().setCreatedBy(productBO.getAuditing().getCreatedBy());
            httpResponse.getAuditing().setCreatedAt(productBO.getAuditing().getCreatedAt());
            httpResponse.getAuditing().setLastModifiedBy(productBO.getAuditing().getLastModifiedBy());
            httpResponse.getAuditing().setLastModifiedAt(productBO.getAuditing().getLastModifiedAt());

            httpResponse.setName(productBO.getName());

            httpResponse.setUnitPrice(productBO.getUnitPrice());
            httpResponse.setUnitOfMeasure(productBO.getUnitOfMeasure());
            httpResponse.setMinSalesQuantity(productBO.getMinSalesQuantity());
            httpResponse.setIncreaseSalesQuantity(productBO.getIncreaseSalesQuantity());
            httpResponse.setDescription(productBO.getDescription());


            if(productBO.getEnabled()!=null){
                httpResponse.setEnabled(productBO.getEnabled() == 1);
            }
            httpResponse.setVersion(productBO.getVersion());
            httpResponse.getAccessoryGroups().addAll(productBO.getAccessoryGroups().stream().map(ProductHttpVOConverter::fromVOToAcc).collect(Collectors.toList()));
            httpResponse.getMethodGroups().addAll(productBO.getMethodGroups().stream().map(ProductHttpVOConverter::fromVOToMeth).collect(Collectors.toList()));
            return httpResponse;
        }
    }

    public static ProductPageHttpResponse fromPageBO(PageBO<ProductBO> pageBO) {
        if (pageBO == null) {
            return null;
        } else {
            ProductPageHttpResponse httpResponse = new ProductPageHttpResponse();
            httpResponse.setPageIndex(pageBO.getPageIndex());
            httpResponse.setPageSize(pageBO.getPageSize());
            httpResponse.setTotalCount(pageBO.getTotalCount());
            httpResponse.setTotalPageCount(pageBO.getTotalPageCount());
            httpResponse.getRecords().addAll(pageBO.getRecords().stream().map(ProductHttpVOConverter::fromPageBORecord).collect(Collectors.toList()));
            return httpResponse;
        }
    }

    public static ProductPageHttpResponse.Record fromPageBORecord(ProductBO productBO) {
        if (productBO == null) {
            return null;
        } else {
            ProductPageHttpResponse.Record record = new ProductPageHttpResponse.Record();
            record.setId(productBO.getId());
            record.setTenantId(productBO.getTenantId());
            record.getAuditing().setCreatedBy(productBO.getAuditing().getCreatedBy());
            record.getAuditing().setCreatedAt(productBO.getAuditing().getCreatedAt());
            record.getAuditing().setLastModifiedBy(productBO.getAuditing().getLastModifiedBy());
            record.getAuditing().setLastModifiedAt(productBO.getAuditing().getLastModifiedAt());
            record.setName(productBO.getName());
            record.setUnitPrice(productBO.getUnitPrice());
            record.setUnitOfMeasure(productBO.getUnitOfMeasure());
            record.setMinSalesQuantity(productBO.getMinSalesQuantity());
            record.setIncreaseSalesQuantity(productBO.getIncreaseSalesQuantity());
            record.setDescription(productBO.getDescription());
            if(productBO.getEnabled()!=null){
                record.setEnabled(productBO.getEnabled() == 1);
            }
            record.setVersion(productBO.getVersion());
            record.getAccessoryGroups().addAll(productBO.getAccessoryGroups().stream().map(ProductHttpVOConverter::fromVOToAcc).collect(Collectors.toList()));
            record.getMethodGroups().addAll(productBO.getMethodGroups().stream().map(ProductHttpVOConverter::fromVOToMeth).collect(Collectors.toList()));
            return record;
        }
    }

    /**
     * 转化 加料
     * @param accessoryGroupBO BO模型
     * @return  ProductDetailHttpResponse.AccessoryGroup
     */
    public static ProductDetailHttpResponse.AccessoryGroup fromVOToAcc( ProductBO.AccessoryGroup accessoryGroupBO){
        ProductDetailHttpResponse.AccessoryGroup accessoryGroupVO=new ProductDetailHttpResponse.AccessoryGroup();
        accessoryGroupVO.setId(accessoryGroupBO.getId());
        accessoryGroupVO.setName(accessoryGroupBO.getName());
        accessoryGroupVO.getOptions().addAll(accessoryGroupBO.getOptions().stream().map(ProductHttpVOConverter::fromVOToAccOption).collect(Collectors.toList()));
        return accessoryGroupVO;
    }
    public static ProductDetailHttpResponse.AccessoryGroup.Option fromVOToAccOption(ProductBO.AccessoryGroup.Option optionBO){
        ProductDetailHttpResponse.AccessoryGroup.Option optionVO=new ProductDetailHttpResponse.AccessoryGroup.Option();
        optionVO.setId(optionBO.getId());
        optionVO.setName(optionBO.getName());
        optionVO.setUnitPrice(optionBO.getUnitPrice());
        optionVO.setUnitOfMeasure(optionBO.getUnitOfMeasure());
        return optionVO;
    }


    /**
     * 转换做法组
     * @param methodGroupBO BO模型
     * @return  ProductDetailHttpResponse.MethodGroup
     */

    public static ProductDetailHttpResponse.MethodGroup fromVOToMeth( ProductBO.MethodGroup methodGroupBO){
        ProductDetailHttpResponse.MethodGroup methodGroupVO=new ProductDetailHttpResponse.MethodGroup();

        methodGroupVO.setName(methodGroupBO.getName());
        methodGroupVO.getOptions().addAll(methodGroupBO.getOptions().stream().map(ProductHttpVOConverter::fromVOToMethOption).collect(Collectors.toList()));
        return methodGroupVO;
    }

    public static ProductDetailHttpResponse.MethodGroup.Option fromVOToMethOption(ProductBO.MethodGroup.Option optionBO){
        ProductDetailHttpResponse.MethodGroup.Option optionVO=new ProductDetailHttpResponse.MethodGroup.Option();
        optionVO.setId(optionBO.getId());
        optionVO.setName(optionBO.getName());
        return optionVO;
    }









}
